/*
 * serial.h
 *
 *  Created on: Jan 16, 2014
 *      Author: yosef
 */

#ifndef SERIAL_H_
#define SERIAL_H_

#include <semaphore.h>

class Serial
{
private:
	// Semaphore
	sem_t *mutex;
	// path name
	char path[25];
	// File directory
    int fd;

public:
    // Constructor of the class
    Serial() : mutex(NULL), fd(0)
	{/* Empty body. */}

    // Destructor
    ~Serial()
    {/* Empty body. */}


    /*! \brief Clean serial resources
     *
     * @param Device Port name (/dev/ttyS0, /dev/ttyACM0, /dev/ttyUSB0)
     */
    void Clean(const char *Device);


    /*! \brief Open the serial port
     *
     * @param Device		Port name (/dev/ttyS0, /dev/ttyACM0, /dev/ttyUSB0)
     * @param Bauds		Baud rate
     * @param DataBit		Data bit width
     * @param Parity		Parity type ("none", "even", "odd", "mark", "space")
     * @param StopBit		Stop bit
     * @param	HwCtrl		Hardware control
     * @return
     * 		0 success
     * 		-1 device not found
     * 		-2 error opening the device
     * 		-3 error writing port parameters
     * 		-4 error invalid port parameters
     * 		-5 error create semaphore
     */
    int Open(const char *Device, const unsigned long int Bauds, const char DataBit,
    		const char *Parity, const char StopBit, bool HwCtrl);


    /*! \brief Close the connection with the current device
    */
    void Close(void);

    /*! \brief Write a byte to serial device.
     *  @param Byte Byte to write.
     *  @return
     *  	0 	success
     *  	-1	error
     */
    int WriteChar(const char Byte);

    /*! \brief Write a string on the current serial port
     *
     * @param String	String to send on the port (must be terminated by '\0')
     * @return
     * 		0 success
     * 		-1 error
     */
    int WriteString(const char *String);

    /*! \brief Write an array of data on the current serial port
     *
     * @param Buffer		Array of bytes to send on the port
     * @param NbBytes		Number of byte to send
     *
     * @return
     * 		0 success
     * 		-1 error
     */
    int Write(const void *Buffer, const unsigned int NbBytes);


    /*! \brief Wait for a byte from the serial device and return the data read
     *
     * @param pByte			data read on the serial device
     * @param TimeOut_ms	delay of timeout before giving up the reading
     * 						If set to zero, timeout is disable (Optional)
     * @return
     * 		1 success
     * 		0 timeout
     * 		-1 error invalid paramter
     */
    int ReadChar(char *pByte, const unsigned int TimeOut_ms);

    /*! \brief Read a string from the serial device (with timeout)
     *
     * @param String		string read on the serial device
     * @param FinalChar		final char of the string
     * @param MaxNbBytes	maximum allowed number of bytes read
     * @param TimeOut_ms	delay of timeout before giving up the reading (optional)
     * @return the number of bytes read
     *		0 timeout
     * 		-1 error invalid parameter
     * 		-2 error reading the byte
     */
    int ReadString(char *String, char FinalChar, unsigned int MaxNbBytes, const unsigned int TimeOut_ms);

    /*! \brief Read a string from the serial device (without TimeOut)
     *
     * @param String		string read on the serial device
     * @param FinalChar		final char of the string
     * @param MaxNbBytes	maximum allowed number of bytes read
     * @return
     * 		>0 success, return the number of bytes read
     * 		0 timeout
     * 		-1 error invalid parameter
     * 		-2 error reading the byte
    */
    int ReadStringNoTimeOut(char *String, char FinalChar, unsigned int MaxNbBytes);

    /*! \brief Read an array of bytes from the serial device (with timeout)
     *
     * @param Buffer		array of bytes read from the serial device
     * @param MaxNbBytes	maximum allowed number of bytes read
     * @param TimeOut_ms	delay of timeout before giving up the reading
     * @return
     * 		>0 success, return the number of bytes read
     * 		0 timeout
     * 		-1 error invalid parameter
     * 		-2 error reading the byte
     */
    int Read(void *Buffer, unsigned int MaxNbBytes, const unsigned int TimeOut_ms);

    /*! \brief  Return the number of bytes in the received buffer (UNIX only)
     * @return The number of bytes in the received buffer
     */
    int Peek();
};



#endif /* SERIAL_H_ */
